package com.lxk.config;

import com.lxk.aop.AdminInterceptor;
import com.lxk.aop.LoginInterceptor;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author JohnSoybean
 * @create 2020-03-21 18:51
 * @description 官方推荐的扩展SpringMVC配置类
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /*
    1. 多个拦截器组成一个拦截器链
    2. addPathPatterns 添加拦截规则
    3. excludePathPatterns 排除拦截地址
    */
    /* 使用@Bean注入，为了使拦截器进入spring框架，以便用autowired调用 */
    @Bean
    public LoginInterceptor setLoginBean(){
        return new LoginInterceptor();
    }
    @Bean
    public AdminInterceptor setAdminBean(){
        return new AdminInterceptor();
    }

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(setLoginBean())
                .addPathPatterns("/**")
                .excludePathPatterns("/login", "/logout", "/checkLogin", "/wxpay/afterrefund",
                        "/bootstrap/**", "/css/**", "/font-awesome/**", "/images/**", "/js/**", "/wangEditor/**", "/chart/**",
                        "/reminder");
        registry.addInterceptor(setAdminBean())
                .addPathPatterns("/admin/**");
    }


    /**
     * 配置错误页面
     * @return
     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return (factory -> {
            ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            factory.addErrorPages(errorPage404);
        });
    }


}
